#!/bin/sh /etc/rc.common
# Copyright (c) 2021 OpenWrt.org

START=19

USE_PROCD=1
PROG=/usr/sbin/bridger

get_defaults() {
	cfg="$1"

	config_get blacklist "$cfg" blacklist
	json_add_array blacklist
	for i in $blacklist; do
		json_add_string "" "$i"
	done
	json_close_array

	config_get_bool bridge_local_tx "$cfg" bridge_local_tx 1
	json_add_boolean bridge_local_tx "$bridge_local_tx"

	config_get_bool bridge_local_rx "$cfg" bridge_local_rx 0
	json_add_boolean bridge_local_rx "$bridge_local_rx"
}

reload_service() {
	config_load bridger

	json_init
	config_foreach get_defaults defaults

	ubus call bridger set_config "$(json_dump)"
}

service_triggers() {
	procd_add_reload_trigger bridger
}

start_service() {
	procd_open_instance
	procd_set_param command "$PROG"
	procd_set_param respawn
	procd_close_instance
}

service_running() {
	ubus -t 10 wait_for bridger
	[ $? = 0 ] && reload_service
}
